import importlib
from pathlib import Path
from typing import List
from .base import BaseModel, TimestampMixin
import logging

logger = logging.getLogger(__name__)


def get_model_files() -> List[str]:
    """获取models目录下所有模型文件名"""
    current_dir = Path(__file__).parent
    model_files = []

    for item in current_dir.glob("*.py"):
        if item.is_file() and item.stem not in ["__init__", "base"]:
            model_files.append(item.stem)

    return model_files


# 自动导入所有模型
model_names = []
for model_file in get_model_files():
    module = importlib.import_module(f"model.{model_file}")
    # 获取模块中所有成员
    for attr_name in dir(module):
        attr = getattr(module, attr_name)
        # 检查是否是 BaseModel 的子类
        if (isinstance(attr, type)
                and issubclass(attr, BaseModel)
                and attr != BaseModel):
            globals()[attr_name] = attr
            model_names.append(attr_name)
            logger.debug(f"Imported model: {attr_name}")

# 导出所有模型
__all__ = ["BaseModel", "TimestampMixin"] + model_names
